<template>
  <el-dialog width="66%" :title="dialogTitle" v-model="dialogVisible" modal @close="closeDialog" draggable
    :close-on-click-modal="false" :append-to-body="true">
    <el-form :inline="true" :model="queryParam" class="demo-form-inline">
      <el-form-item label="档号/题名">
        <el-input size="small" v-model="queryParam.archiveNoOrName" placeholder="请输入档号或题名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" border style="width: 100%" height="35vh">
      <el-table-column type="index" label="序号" align="center" width="50"></el-table-column>
      <el-table-column v-for="item of tableProps" :key="item.props" :prop="item.prop" :label="item.title"
        :min-width="item.width">
        <template #default="scope">
          <span v-if="item.prop === 'stageStatus'">
            <el-tag v-if="scope.row.stageStatus == 0">未上架</el-tag>
            <el-tag v-if="scope.row.stageStatus == 1" type="success">已上架</el-tag>
            <el-tag v-if="scope.row.stageStatus == 2" type="danger">已下架</el-tag>
          </span>
          <span v-else-if="item.prop === 'storeStatus'">
            <el-tag :type="getStatusType(scope.row)">
              {{ getStatusText(scope.row) }}
            </el-tag>
          </span>
          <span v-else>{{ scope.row[item.prop] }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template #default="scope">
          <el-button link @click="handleEdit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination ref="pagination" style="margin-top: 10px" :totalCount="totalCount" @pagination="pagination">
    </pagination>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog" size="mini">关 闭</el-button>
    </div>
    <new-archives-dialog ref="newArchivesDialog" @ok="loadData"></new-archives-dialog>
  </el-dialog>
</template>

<script>
import { pageByBoxNo } from '@/api/rfid/label'
import newArchivesDialog from './newArchivesDialog.vue'
import { rfidAccountFieldDefinelistArchive } from '@/api/sys/field'
import { getDictItemsByCodes } from '@/api/sys/dict'

export default {
  name: 'archivesByBoxNo',
  components: { newArchivesDialog },
  data() {
    return {
      dialogTitle: '',
      dialogVisible: false,
      tableData: [],
      totalCount: 0, //总数
      //查询条件
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      queryParam: {},
      tableProps: [],
      dictProps: [],

      // tableProps: [
      //   { title: '档号', prop: 'archiveNo', width: '', checked: true },
      //   { title: '题名', prop: 'archiveName', width: '100', checked: true },
      //   { title: 'RFID', prop: 'rfid', width: '', checked: true },
      //   { title: '页数', prop: 'pageNo', width: '', checked: true },
      //   { title: '上架状态', prop: 'stageStatus', width: '', checked: true },
      //   { title: '在库状态', prop: 'storeStatus', width: '', checked: true },
      // ],
    }
  },
  methods: {
    toCamelCase(str) {
      // 首先将字符串分割成单词数组
      const words = str.split(/[-_\s]+/)

      // 如果字符串为空，返回空字符串
      if (words.length === 0) {
        return ''
      }

      // 将第一个单词小写，其余单词首字母大写，然后连接它们
      return (
        words[0].toLowerCase() +
        words
          .slice(1)
          .map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
          .join('')
      )
    },
    getArchiveFields() {
      rfidAccountFieldDefinelistArchive()
        .then((res) => {
          if (res.data.code === 200) {
            this.tableProps = res.data.data.map((item) => {
              return {
                title: item.fieldTitle,
                prop: this.toCamelCase(item.fieldName),
                width: '',
                checked: true,
                showOverflowTooltip: true,
              }
            })
            // 将res.data.data中dictCode不为null的提取出来，生成一个对象数组，对象中包含fieldName和dictCode
            this.dictProps = res.data.data
              .filter((item) => item.dictCode !== null && item.dictCode !== '')
              .map((item) => {
                return {
                  fieldName: this.toCamelCase(item.fieldName),
                  dictCode: item.dictCode,
                }
              })
            if (this.dictProps.length > 0) {
              const dictCodes = this.dictProps.map((item) => item.dictCode)
              this.getDictItems(dictCodes)
            }
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    // 根据字典编码数组获取字典值
    getDictItems(dictCodes) {
      getDictItemsByCodes(dictCodes)
        .then((res) => {
          if (res.data.code === 200) {
            this.dictItems = res.data.data
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    handleEdit(row) {
      this.$refs.newArchivesDialog.edit(row)
    },
    init(row) {
      this.dialogTitle = row.boxNo
      this.queryParam.boxNo = row.boxNo
      this.getArchiveFields()
      this.dialogVisible = true
      this.loadData()
    },
    onSubmit() {
      this.$refs.pagination.resetPagination()
    },
    // 根据 storeStatus 和 stageStatus 获取标签类型
    getStatusType(row) {
      if (row.storeStatus === 0) return ''
      if (row.storeStatus === 1) return row.stageStatus === 1 ? 'success' : 'warning'
      if (row.storeStatus === 2) return 'danger'
      return ''
    },

    // 根据 storeStatus 和 stageStatus 获取标签文本
    getStatusText(row) {
      if (row.storeStatus === 0) return '未入库'
      if (row.storeStatus === 1) return row.stageStatus === 1 ? '在库' : '暂存'
      if (row.storeStatus === 2) return '离库'
      return ''
    },
    loadData() {
      const param = Object.assign(this.queryParam, this.searchModel)
      pageByBoxNo(param)
        .then((res) => {
          if (res.data.code === 200) {
            this.tableData = res.data.data.records
            this.totalCount = res.data.data.total
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    pagination(val) {
      this.searchModel.pageNo = val.page
      this.searchModel.pageSize = val.limit
      this.loadData()
    },
    closeDialog() {
      this.dialogVisible = false
      this.queryParam = {}
    },
  },
}
</script>

<style scoped></style>
